<?php

class validation {

    protected $message;

    public function __construct() {
        $this->message = '';
    }

    public static function validEmail($str) {
        return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
    }

    public static function alphaNumeric($str) {
        return (!preg_match("/^([a-z0-9_])+$/i", $str)) ? FALSE : TRUE;
    }

    public function numeric($str) {
        return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
    }

    public function integer($str) {
        return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
    }

    public function isNatural($str) {
        return (bool) preg_match('/^[0-9]+$/', $str);
    }

    public static function greater0($str) {
        if (!preg_match('/^[0-9]+$/', $str)) {
            return FALSE;
        }

        if ($str == 0) {
            return FALSE;
        }

        return TRUE;
    }

    public static function validPassword($str) {
        return (!preg_match("/^([a-z0-9_]{4,})+$/i", $str)) ? FALSE : TRUE;
    }

    public static function validUsername($str) {
        return (!preg_match("/^([a-z0-9_]{4,})+$/i", $str)) ? FALSE : TRUE;
    }

    public static function notEmpty($str) {
        if (trim($str) == '') {
            return false;
        }

        return true;
    }

    public static function date($str) {
        return (!preg_match("/^(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)\d\d$/", $str)) ? FALSE : TRUE;
    }

    public static function equalText($str1, $str2) {
        $str1 = trim($str1);
        $str2 = trim($str2);
        if (!(bool) strcmp($str1, $str2)) {
            return TRUE;
        }

        return FALSE;
    }

    public static function minLength($str, $num) {
        $str = trim($str);
        if (strlen($str) > $num) {
            return TRUE;
        }

        return FALSE;
    }

    public static function birthday($str) {
        $arrMonth = array(
            1 => 'January',
            2 => 'February',
            3 => 'March',
            4 => 'April',
            5 => 'May',
            6 => 'Jun',
            7 => 'July',
            8 => 'August',
            9 => 'September',
            10 => 'October',
            11 => 'November',
            12 => 'December'
        );
        if (self::date($str)) {
            list($day, $month, $year) = explode('-', $str);
            //list($month, $day, $year) = explode('-', $str);
            switch ($month) {
                case 2: {
                        if ($day > self::getDaysInMonth($month, $year)) {
                            //return 1;
                            return $arrMonth[$month] . "only 28 days";
                        }
                    }
                    break;
                case 4:
                case 6:
                case 9:
                case 11: {
                        if ($day >= 31) {
                            //return 2;
                            return $arrMonth[$month] . " only 30 days";
                        }
                    }
                    break;
            }
        } else {
            //return -1;
            return 'Birthday is invalid. Birthday must in dd/mm/yy';
        }
        return '';
    }

    public static function getDaysInMonth($month, $year) {
        return $month == 2 ?
                ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) :
                (($month - 1) % 7 % 2 ? 30 : 31);
    }

}

?>